/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.login;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassUtility;

/**
 *
 * @author Hendro Steven
 */
public class LoginDAOImpl implements LoginDAO {

    public LoginDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    public String getKodePegawai(String userName) throws Exception {
        String sql = "SELECT kdPegawai FROM personalia.pegawai WHERE npp=? limit 1";
        try {
            return ClassAntiNull.AntiNullString(ClassConnection.getJdbc().queryForObject(sql, new Object[]{
                        userName
                    }, String.class));
        } catch (Exception ex) {
            return "";
        }
    }

    public Map getUserGroup(String kdPegawai) throws Exception {
        String sql = "SELECT userGroup.NomorUser, userGroup.GroupMenuID, userGroup.Tabel, userGroup.StatusUser, groupMenu.MenuID "
                + "FROM siUSD.groupMenu INNER JOIN siUSD.userGroup ON (groupMenu.GroupMenuID = userGroup.GroupMenuID) "
                + "WHERE (userGroup.NomorUser = ? AND userGroup.StatusUser ='Y' AND groupMenu.MenuID =1)";
        try {
            Map map = ClassConnection.getJdbc().queryForMap(sql, new Object[]{kdPegawai});
            return map;
        } catch (Exception ex) {
            return null;
        }
    }

    public Map login(String table, String user, String password, boolean isPegawai) throws Exception {
        String sql = "";
        if (isPegawai) {
            sql = "SELECT * FROM " + table + " WHERE username=? AND password=?";
        } else {
            sql = "SELECT * FROM " + table + " WHERE Nomor_mhs=? AND Sandi=?";
        }
        try {
            Map map = ClassConnection.getJdbc().queryForMap(sql, new Object[]{
                        user, password
                    });
            return map;
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Menu> createMenu() throws Exception {

        List<Menu> list = new ArrayList<Menu>();
        String sql = "SELECT menu_id, menu_application_id, menu_parent, menu_order, menu_caption, menu_action "
                + "FROM siUSD.menu WHERE menu_application_id =1 ORDER BY menu_id ASC, menu_order ASC";
        try{
            List<Map> map = ClassConnection.getJdbc().queryForList(sql,new Object[]{});
            for(Map m : map){
                Menu menu = new Menu();
                menu.setId(ClassAntiNull.AntiNullInt(m.get("menu_id")));
                menu.setParent(ClassAntiNull.AntiNullInt(m.get("menu_parent")));
                menu.setCaption(ClassAntiNull.AntiNullString(m.get("menu_caption")));
                menu.setAction(ClassAntiNull.AntiNullString(m.get("menu_action")));
                list.add(menu);
            }
        }catch(Exception ex){
            
        }
        return list;
    }
}
